package com.youku.plugins.toolboxV4.view
{
   import flash.display.Sprite;
   import com.youku.baseUI.YoukuTextField;
   import fl.controls.RadioButtonGroup;
   import flash.utils.Dictionary;
   import flash.events.Event;
   import com.youku.utils.AssetUtil;
   import com.youku.plugins.toolboxV4.ToolboxFont;
   import flash.text.TextFieldAutoSize;
   import flash.events.MouseEvent;
   import com.youku.utils.ReportUtil;
   import fl.controls.ButtonLabelPlacement;
   import com.youku.interfaces.IDVDData;
   
   public class DvdPanel extends Sprite
   {
      
      public function DvdPanel() {
         super();
         this.init();
      }
      
      private var _title:YoukuTextField;
      
      private var _othertitle:YoukuTextField;
      
      private var _tbGroup:RadioButtonGroup;
      
      private var _tbDefer:Array;
      
      private var _tbSelectors:Dictionary;
      
      private var _multiVoices:Array;
      
      private function init() : void {
         this._multiVoices = [];
         this._tbDefer = [];
         this._tbSelectors = new Dictionary();
         this._tbGroup = new RadioButtonGroup("dvd");
         this._tbGroup.addEventListener(Event.CHANGE,this.onDvdChange);
         this._title = AssetUtil.getYoukuTextField(false);
         this._title.defaultTextFormat = ToolboxFont.FONT_COMMON;
         this._title.autoSize = TextFieldAutoSize.LEFT;
         this._title.fontHoffset = -2;
         this._title.text = "语";
         this.addChild(this._title);
         this._othertitle = AssetUtil.getYoukuTextField(false);
         this._othertitle.defaultTextFormat = ToolboxFont.FONT_COMMON;
         this._othertitle.autoSize = TextFieldAutoSize.LEFT;
         this._othertitle.fontHoffset = -2;
         this._othertitle.text = "言:";
         this.addChild(this._othertitle);
         this.resetOptions();
      }
      
      private function onSelectorClick(param1:MouseEvent) : void {
         var _loc2_:ToolBoxRadioButton = param1.target as ToolBoxRadioButton;
         var _loc3_:Object = {};
         _loc3_.lang = _loc2_.value;
         ReportUtil.sendUserActionLog(ReportUtil.SETTINGS_PANEL_VOICE_SELECTOR_CLICK,ReportUtil.METHOD_LEFT_CLICK,_loc3_);
      }
      
      private function onDvdChange(param1:Event) : void {
         var _loc2_:ToolBoxRadioButton = null;
         for each(_loc2_ in this._tbSelectors)
         {
            if(_loc2_ == this._tbGroup.selection)
            {
               _loc2_.selected = true;
            }
            else
            {
               _loc2_.selected = false;
            }
         }
      }
      
      private function resetOptions() : void {
         var _loc1_:ToolBoxRadioButton = null;
         var _loc2_:* = NaN;
         var _loc3_:ToolBoxRadioButton = null;
         var _loc4_:* = 0;
         var _loc5_:ToolBoxRadioButton = null;
         for each(_loc1_ in this._tbSelectors)
         {
            if(contains(_loc1_))
            {
               removeChild(_loc1_);
            }
         }
         this._tbSelectors = new Dictionary();
         _loc2_ = this._multiVoices.length > 5?5:this._multiVoices.length;
         if(_loc2_ < 2)
         {
            _loc3_ = new ToolBoxRadioButton();
            _loc3_.group = this._tbGroup;
            _loc3_.costomTextFormat = ToolboxFont.FONT_SELECTOR;
            _loc3_.labelPlacement = ButtonLabelPlacement.RIGHT;
            _loc3_.fontHoffset = -2;
            _loc3_.text = "不可选";
            _loc3_.label = "";
            _loc3_.value = "notallow";
            this._tbSelectors["notallow"] = _loc3_;
            _loc3_.enabled = false;
            addChild(_loc3_);
         }
         else
         {
            _loc4_ = 0;
            while(_loc4_ < _loc2_)
            {
               _loc5_ = new ToolBoxRadioButton();
               _loc5_.group = this._tbGroup;
               _loc5_.costomTextField.defaultTextFormat = ToolboxFont.FONT_SELECTOR;
               _loc5_.labelPlacement = ButtonLabelPlacement.RIGHT;
               _loc5_.text = this._multiVoices[_loc4_];
               _loc5_.label = "";
               _loc5_.value = this._multiVoices[_loc4_];
               _loc5_.fontHoffset = -2;
               this._tbSelectors[this._multiVoices[_loc4_]] = _loc5_;
               _loc5_.addEventListener(MouseEvent.CLICK,this.onSelectorClick);
               addChild(_loc5_);
               _loc4_++;
            }
         }
         this.updateLayout();
      }
      
      private function updateLayout() : void {
         var _loc2_:* = 0;
         this._title.x = 11;
         this._title.y = 4;
         this._othertitle.x = 47;
         this._othertitle.y = 4;
         var _loc1_:Number = this._multiVoices.length > 5?5:this._multiVoices.length;
         if(_loc1_ < 2)
         {
            this._tbSelectors["notallow"].setWH(56,18);
            this._tbSelectors["notallow"].x = this._othertitle.x + this._othertitle.width + 6;
         }
         else
         {
            _loc2_ = 0;
            while(_loc2_ < _loc1_)
            {
               this._tbSelectors[this._multiVoices[_loc2_]].setWH(44,18);
               if(_loc2_ == 0)
               {
                  this._tbSelectors[this._multiVoices[_loc2_]].x = this._othertitle.x + this._othertitle.width + 6;
               }
               else
               {
                  this._tbSelectors[this._multiVoices[_loc2_]].x = this._tbSelectors[this._multiVoices[_loc2_ - 1]].x + this._tbSelectors[this._multiVoices[_loc2_ - 1]].width;
               }
               this._tbSelectors[this._multiVoices[_loc2_]].y = 0;
               _loc2_++;
            }
         }
      }
      
      public function setVoice(param1:String) : void {
         var _loc3_:ToolBoxRadioButton = null;
         if(param1 == "")
         {
            return;
         }
         var _loc2_:* = 0;
         while(_loc2_ < this._multiVoices.length)
         {
            _loc3_ = this._tbSelectors[this._multiVoices[_loc2_]];
            if((_loc3_) && _loc3_.value == param1)
            {
               _loc3_.selected = true;
            }
            _loc2_++;
         }
      }
      
      public function getVoice() : String {
         var _loc1_:* = "default";
         if(this._tbGroup.selection)
         {
            _loc1_ = this._tbGroup.selection.value as String;
         }
         if(_loc1_ == "notallow")
         {
            _loc1_ = "default";
         }
         return _loc1_;
      }
      
      public function setData(param1:IDVDData) : void {
         if(param1 == null)
         {
            return;
         }
         this._multiVoices = [];
         var _loc2_:* = 0;
         while(_loc2_ < param1.audioLangs.length)
         {
            this._multiVoices.push(param1.audioLangs[_loc2_].lang);
            _loc2_++;
         }
         this.resetOptions();
      }
   }
}
